package com.filldream.fastboot.common.stockUtils.qt.entity;

import com.filldream.fastboot.common.constant.enums.StockMarketUtil;
import com.filldream.fastboot.common.entity.db.StockMarket;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 股票基本信息
 * 参考博客：
 * https://blog.csdn.net/zchill/article/details/121303871
 * 终极参考博客：
 * https://blog.csdn.net/afgasdg/article/details/84064421
 * 参考API：
 * https://tpdog.com/api/stock_his/daily?code=sz.002456&start=2024-04-08&end=2024-04-09
 * https://tpdog.com/api/board/bill?date=2024-01-17
 * http://tpdog.com/api/pool/limitup/list?date=2024-01-17
 * http://tpdog.com/api/indicator/macd?code=sz.000001&period=10&macd_long=26&macd_short=12&macd_mid=9&date=2024-01-17
 * https://qt.gtimg.cn/q=sz002456
 * https://qt.gtimg.cn/q=s_sz002456
 * https://qt.gtimg.cn/q=ff_sz002456
 */
@Data
public class StockInfo {

    @ApiModelProperty(value = "交易所类型")
    private String marketType;

    @ApiModelProperty(value = "交易所名称")
    private String address;

    @ApiModelProperty(value = "名称")
    private String name;

    @ApiModelProperty(value = "代码")
    private String code;

    @ApiModelProperty(value = "腾讯搜索代码")
    private String searchCode;

    @ApiModelProperty(value = "实时价格")
    private BigDecimal nowPrice;

    @ApiModelProperty(value = "昨收")
    private BigDecimal yesterdayPrice;

    @ApiModelProperty(value = "开盘价格")
    private BigDecimal openPrice;

    @ApiModelProperty(value = "成交量(手)")
    private BigDecimal dealCount;

    @ApiModelProperty(value = "外盘")
    private BigDecimal outSum;

    @ApiModelProperty(value = "内盘")
    private BigDecimal innerSum;

    @ApiModelProperty(value = "买1价")
    private BigDecimal buy1Price;

    @ApiModelProperty(value = "买1量(手)")
    private BigDecimal buy1Count;

    @ApiModelProperty(value = "买2价")
    private BigDecimal buy2Price;

    @ApiModelProperty(value = "买2量(手)")
    private BigDecimal buy2Count;

    @ApiModelProperty(value = "买3价")
    private BigDecimal buy3Price;

    @ApiModelProperty(value = "买3量(手)")
    private BigDecimal buy3Count;

    @ApiModelProperty(value = "买4价")
    private BigDecimal buy4Price;

    @ApiModelProperty(value = "买4量(手)")
    private BigDecimal buy4Count;

    @ApiModelProperty(value = "买5价")
    private BigDecimal buy5Price;

    @ApiModelProperty(value = "买5量(手)")
    private BigDecimal buy5Count;

    @ApiModelProperty(value = "卖1价")
    private BigDecimal sell1Price;

    @ApiModelProperty(value = "卖1量(手)")
    private BigDecimal sell1Count;

    @ApiModelProperty(value = "卖2价")
    private BigDecimal sell2Price;

    @ApiModelProperty(value = "卖2量(手)")
    private BigDecimal sell2Count;

    @ApiModelProperty(value = "卖3价")
    private BigDecimal sell3Price;

    @ApiModelProperty(value = "卖3量(手)")
    private BigDecimal sell3Count;

    @ApiModelProperty(value = "卖4价")
    private BigDecimal sell4Price;

    @ApiModelProperty(value = "卖4量(手)")
    private BigDecimal sell4Count;

    @ApiModelProperty(value = "卖5价")
    private BigDecimal sell5Price;

    @ApiModelProperty(value = "卖5量(手)")
    private BigDecimal sell5Count;

    @ApiModelProperty(value = "最近逐笔成交")
    private String everyDealInfo;

    @ApiModelProperty(value = "时间字符")
    private String dateTimeStr;

    @ApiModelProperty(value = "涨跌额")
    private BigDecimal zde;

    @ApiModelProperty(value = "涨幅")
    private BigDecimal zf;

    @ApiModelProperty(value = "最高价格")
    private BigDecimal hPrice;

    @ApiModelProperty(value = "最低价格")
    private BigDecimal lPrice;

    @ApiModelProperty(value = "价格/成交量（手）/成交额")
    private String priceCountMoneyDealInfo;

    @ApiModelProperty(value = "成交额(万)")
    private BigDecimal dealMoney;

    @ApiModelProperty(value = "换手率(%)")
    private BigDecimal hsl;

    @ApiModelProperty(value = "市盈率(TTM)")
    private BigDecimal syl;

    @ApiModelProperty(value = "振幅(%)")
    private BigDecimal amplitude;

    @ApiModelProperty(value = "流通市值(亿)")
    private BigDecimal circulationMarketPrice;

    @ApiModelProperty(value = "市值(亿)")
    private BigDecimal marketPrice;

    @ApiModelProperty(value = "市净率")
    private BigDecimal sjl;

    @ApiModelProperty(value = "涨停价")
    private BigDecimal stopHPrice;

    @ApiModelProperty(value = "跌停价")
    private BigDecimal stopLPrice;

    @ApiModelProperty(value = "量比")
    private BigDecimal lb;

    @ApiModelProperty(value = "委差")
    private BigDecimal wc;

    @ApiModelProperty(value = "均价")
    private BigDecimal avgPrice;

    @ApiModelProperty(value = "市盈率动态")
    private BigDecimal sylDynamic;

    @ApiModelProperty(value = "市盈率静态")
    private BigDecimal sylStatic;

    //计算后的参数
    @ApiModelProperty(value = "内外盘差距")
    private BigDecimal diffSum;

    @ApiModelProperty(value = "腾讯返回的原结果")
    private String urlResult;

    @ApiModelProperty(value = "涨跌状态 -1为跌 0-不变 1-涨")
    public Integer zhangFlag = 0;

    @ApiModelProperty(value = "买盘Level5行情列表")
    public List<Level5DTO> buyLevel5List;

    @ApiModelProperty(value = "卖盘Level5行情列表")
    public List<Level5DTO> sellLevel5List;

    public StockInfo init(String stockInfo){
        //取出参数
        String[] params = stockInfo.split("~");
        this.urlResult = stockInfo;
        this.marketType = params[0];
        this.address = StockMarketUtil.findByTxType(this.marketType).getMarketName();
        this.name = params[1];
        this.code = params[2];
        this.nowPrice = new BigDecimal(params[3]);
        this.yesterdayPrice = new BigDecimal(params[4]);
        this.openPrice = new BigDecimal(params[5]);
        this.dealCount = toBigDecimal(params[6]);
        this.outSum = toBigDecimal(params[7]);
        this.innerSum = toBigDecimal(params[8]);;
        this.buy1Price = new BigDecimal(params[9]);
        this.buy1Count = new BigDecimal(params[10]);
        this.buy2Price = new BigDecimal(params[11]);
        this.buy2Count = new BigDecimal(params[12]);
        this.buy3Price = new BigDecimal(params[13]);
        this.buy3Count = new BigDecimal(params[14]);
        this.buy4Price = new BigDecimal(params[15]);
        this.buy4Count = new BigDecimal(params[16]);
        this.buy5Price = new BigDecimal(params[17]);
        this.buy5Count = new BigDecimal(params[18]);
        this.sell1Price = new BigDecimal(params[19]);
        this.sell1Count = new BigDecimal(params[20]);
        this.sell2Price = new BigDecimal(params[21]);
        this.sell2Count = new BigDecimal(params[22]);
        this.sell3Price = new BigDecimal(params[23]);
        this.sell3Count = new BigDecimal(params[24]);
        this.sell4Price = new BigDecimal(params[25]);
        this.sell4Count = toBigDecimal(params[26]);
        this.sell5Price = toBigDecimal(params[27]);
        this.sell5Count = toBigDecimal(params[28]);
        this.everyDealInfo = params[29];
        this.dateTimeStr = params[30];
        this.zde = toBigDecimal(params[31]);
        this.zf = toBigDecimal(params[32]);
        this.hPrice = toBigDecimal(params[33]);
        this.lPrice = toBigDecimal(params[34]);
        this.priceCountMoneyDealInfo = params[35];
        //36也是成交量，以后注意区别。目前无区别
        this.dealMoney = toBigDecimal(params[37]);
        this.hsl = toBigDecimal(params[38]);
        this.syl = toBigDecimal(params[39]);
        //40未知
        //41也是最高价，以后注意区别。目前无区别
        //42也是最低价，以后注意区别。目前无区别
        this.amplitude = new BigDecimal(params[43]);
        this.circulationMarketPrice = toBigDecimal(params[44]);
        this.marketPrice =toBigDecimal(params[45]);
        if( !this.marketType.equals("200") ){
            this.sjl = this.marketType.equals("100") ? toBigDecimal(params[58]) : toBigDecimal(params[46]);
        }
        this.stopHPrice = toBigDecimal(params[47]);
        this.stopLPrice = toBigDecimal(params[48]);
        this.lb = new BigDecimal(params[49]);
        this.wc = new BigDecimal(params[50]);
        this.avgPrice = toBigDecimal(params[51]);
        this.sylDynamic = toBigDecimal(params[52]);
        this.sylStatic = toBigDecimal(params[53]);

        //计算后的参数
        this.diffSum = outSum.subtract(innerSum);
        this.zhangFlag = zf.compareTo(BigDecimal.ZERO);

        //获取腾讯搜索代码
        StockMarket byTxType = StockMarketUtil.findByTxType(this.marketType);
        this.searchCode = byTxType != null ? byTxType.getCodePrefix() + code : code;
        this.searchCode = this.searchCode.replaceAll(".OQ", "");

        //处理level5
        this.buyLevel5List = initLevel5(this,true);
        this.sellLevel5List = initLevel5(this,false);

        if( this.name.equals("GC001") ){
            this.name = "沪市一天期";
        }else if( this.name.equals("纳斯达克中国金龙指数") ){
            this.name = "纳指中国金龙";
        }

        return this;
    }

    private BigDecimal toBigDecimal(String str){
        return StringUtils.isBlank(str) ? null : new BigDecimal(str);
    }

    /**
     * 初始化level5数据
     * @param stockInfo 股票基本数据
     * @param buyFlag   是否买
     * @return
     */
    public List<Level5DTO> initLevel5(StockInfo stockInfo,Boolean buyFlag){
        List<Level5DTO> result = new ArrayList<>();
        for (int i = 1; i < 6; i++) {
            Level5DTO item = new Level5DTO();
            item.setBuyFlag(buyFlag);
            item.setName( (buyFlag ? "买" : "卖") + i);
            item.setPrice( stockInfo.getBuySellMoney(i,buyFlag) );
            item.setCount( stockInfo.getBuySellCount(i,buyFlag) );
            item.setMoney( item.getPrice().multiply(item.getCount()).multiply(new BigDecimal("100")) );
            item.autoHm();
            result.add(item);
        }
        return result;
    }

    /**
     * 获取Leve5的挂单手数
     * @param n Level档次
     * @param buyFlag   是否买
     * @return
     */
    public BigDecimal getBuySellCount(Integer n,Boolean buyFlag){
        BigDecimal tempCount = null;
        if( n.equals(1) ){
            tempCount = buyFlag ? buy1Count : sell1Count;
        }else if( n.equals(2) ){
            tempCount = buyFlag ? buy2Count : sell2Count;
        }else if( n.equals(3) ){
            tempCount = buyFlag ? buy3Count : sell3Count;
        }else if( n.equals(4) ){
            tempCount = buyFlag ? buy4Count : sell4Count;
        }else if( n.equals(5) ){
            tempCount = buyFlag ? buy5Count : sell5Count;
        }
        return tempCount;
    }


    /**
     * 获取Leve5的挂单金额
     * @param n Level档次
     * @param buyFlag   是否买
     * @return
     */
    public BigDecimal getBuySellMoney(Integer n,Boolean buyFlag){
        if( n.equals(1) ){
            return buyFlag ? buy1Price : sell1Price;
        }else if( n.equals(2) ){
            return buyFlag ? buy2Price : sell2Price;
        }else if( n.equals(3) ){
            return buyFlag ? buy3Price : sell3Price;
        }else if( n.equals(4) ){
            return buyFlag ? buy4Price : sell4Price;
        }else if( n.equals(5) ){
            return buyFlag ? buy5Price : sell5Price;
        }
        return null;
    }

}
